/*
 * DateTest.java
 * JMUnit based test
 *
 * Created on 7-Nov-2009, 10:02:01 PM
 */
package ocbn.remotelab.test_package;

import ocbn.remotelab.util.date.*;
import java.util.Calendar;
import jmunit.framework.cldc10.*;

/**
 * @author Ke Li
 */
public class DateTest extends TestCase {

    Calendar cal;
    String dateString;

    public DateTest() {
        //The first parameter of inherited constructor is the number of test
        // cases
        super(4, "DateTest");
        cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, 2009);
        cal.set(Calendar.MONTH, 9);
        cal.set(Calendar.DAY_OF_MONTH, 3);
        cal.set(Calendar.HOUR, 10);
        cal.set(Calendar.AM_PM, Calendar.PM);
        cal.set(Calendar.MINUTE, 12);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        dateString = "Oct 3, 2009 10:12:00 PM";
    }

    public void test(int testNumber) throws Throwable {
        switch (testNumber) {
            case 0:
                testToString();
                break;
            case 1:
                testValueOf();
                break;
            case 2:
                testConstructorConsistency();
            case 3:
                testValueOfConsistency();
            default:
                break;
        }
    }

    /**
     * Test of testToString method, of class Date.
     */
    public void testToString() throws AssertionFailedException {
        System.out.println("toString");
        Date instance = new Date(cal.getTime());
        String expResult_1 = dateString;
        String result_1 = instance.toString();
        assertEquals(expResult_1, result_1);
    }

    /**
     * Test of testValueOf method, of class Date.
     */
    public void testValueOf() throws AssertionFailedException {

        System.out.println("valueOf");

        Date expResult_1 = new Date(cal.getTime());
        Date result_1 = Date.valueOf(dateString);
        assertEquals(expResult_1.getTime(), result_1.getTime());
    }

    // The following two test cases are tested because Date might inherit
    // some of the values of the fields of system time at the time of object
    // creation. Therefore, a delay is inserted between the two object creation
    // statements. 

    public void testConstructorConsistency() throws AssertionFailedException {
        Date a = new Date(cal.getTime());
        // Delay for some time
        for(int i = 0; i < 500000; i++);
        Date b = new Date(cal.getTime());
        assertEquals(a, b);

    }

    public void testValueOfConsistency() throws AssertionFailedException {
        Date a = Date.valueOf(dateString);
        // Delay for some time
        for(int i = 0; i < 500000; i++);
        Date b = Date.valueOf(dateString);
        assertEquals(a, b);

    }

}